"""
Case Type   : checksum
Case Name   : 表为mot表，执行checksum语句
Create At   : 2024.09.23
Owner       : @zhanghuan96
Description :
    1、创建兼容b库并查询插件是否加载成功
    2、建表插入数据
    3、建foreign表插入数据
    4、checksum
    5、清理环境
Expect      :
    1、成功，插件查询成功
    2、成功
    3、成功
    4、成功
    5、清理成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_checksum_0011'
        self.tb_name = 'tb_checksum_0011'

    def test_create_function(self):
        self.log.info('--前置条件:设置参数--')
        msg = self.sh_primary.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            'enable_incremental_checkpoint=off')
        self.log.info(msg)
        self.assertTrue(msg)
        msg = self.sh_primary.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            'enable_mot_server=on')
        self.log.info(msg)
        self.assertTrue(msg)
        msg = self.sh_primary.restart_db_cluster()
        self.log.info(msg)
        status = self.sh_primary.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

        text = '-----step1:创建兼容b库库并查询插件;expect:成功-----'
        self.log.info(text)
        sql_cmd = (f"drop database if exists {self.db_name};"
                   f"create database {self.db_name} dbcompatibility ='B';")
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEqual(1,
                         sql_res.count(self.constant.CREATE_DATABASE_SUCCESS),
                         '兼容B库创建失败')
        sql_cmd1 = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.log.info(sql_res1)
        self.assertIn('dolphin', sql_res1, '查询失败')

        text = '-----step2:建表插入数据;expect:成功-----'
        self.log.info(text)
        sql_cmd2 = (f"drop table if exists {self.tb_name};"
                    f"create table {self.tb_name}(c1 int,c2 numeric,c3 text);"
                    f"insert into  {self.tb_name} values(10,3.14,'嘿嘿'),"
                    f"(200,'123.456','开门'),(300,2356,'芝麻');")
        self.log.info(sql_cmd2)
        sql_res2 = self.sh_primary.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.log.info(sql_res2)
        self.assertIn('CREATE TABLE', sql_res2, '创建失败')
        self.assertIn('INSERT', sql_res2, '创建失败')

        text = '-----step3:建foreign表插入数据;expect:成功-----'
        self.log.info(text)
        sql_cmd3 = (f"drop foreign table if exists {self.tb_name}_01;"
                    f"create foreign table {self.tb_name}_01"
                    f"(c1 int,c2 numeric,c3 varchar2(50));"
                    f"insert into  {self.tb_name}_01 values(10,3.14,'嘿嘿'),"
                    f"(200,'123.456','开门'),(300,2356,'芝麻');")
        self.log.info(sql_cmd3)
        sql_res3 = self.sh_primary.execut_db_sql(sql_cmd3, dbname=self.db_name)
        self.log.info(sql_res3)
        self.assertIn('CREATE FOREIGN TABLE', sql_res3, '创建失败')
        self.assertIn('INSERT', sql_res3, '创建失败')

        text = '-----step4:checksum;expect:成功-----'
        self.log.info(text)
        sql_cmd4 = f"checksum table {self.tb_name},{self.tb_name}_01;"
        self.log.info(sql_cmd4)
        sql_res4 = self.sh_primary.execut_db_sql(sql_cmd4, dbname=self.db_name)
        self.log.info(sql_res4)
        self.assertIn('(2 rows)', sql_res4)

    def tearDown(self):
        text = '-----step5:清理环境;expect:成功-----'
        self.log.info(text)
        sql_cmd5 = f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd5)
        sql_res5 = self.sh_primary.execut_db_sql(sql_cmd5, dbname='postgres')
        self.log.info(sql_res5)
        guc_cmd = self.sh_primary.execute_gsguc('set',
                                                self.constant.GSGUC_SUCCESS_MSG,
                                                f'enable_incremental_checkpoint'
                                                f'=on')
        self.log.info(guc_cmd)
        guc_cmd = self.sh_primary.execute_gsguc('set',
                                                self.constant.GSGUC_SUCCESS_MSG,
                                                f'enable_mot_server'
                                                f'=off')
        self.log.info(guc_cmd)
        text = '-----checkpoint;expect:成功-----'
        self.log.info(text)
        sql_cmd6 = f"checkpoint;"
        self.log.info(sql_cmd6)
        sql_res6 = self.sh_primary.execut_db_sql(sql_cmd6, dbname='postgres')
        self.log.info(sql_res6)
        self.assertIn('CHECKPOINT', sql_res6)
        msg = self.sh_primary.restart_db_cluster()
        self.log.info(msg)
        status = self.sh_primary.get_db_cluster_status(param='detail')
        self.log.info(status)
        self.assertTrue(status.count("Degraded") + status.count("Normal") == self.sh_primary.get_node_num() + 1)
        self.assertEqual(1,
                         sql_res5.count(self.constant.DROP_DATABASE_SUCCESS),
                         '清理环境失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
